package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import com.example.demo.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/end/user")
public class EndUserController {


    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @RequestMapping("/add")
    public Object add(@RequestBody User user){
        userMapper.insert(user);
        return LayuiResult.addSuccess();
    }

    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id){
        User user = new User();
        user.setId(id);
        user.setStatus(0);
        userMapper.updateById(user);
        return LayuiResult.deleteSuccess();
    }

    @RequestMapping("/update")
    public Object update(@RequestBody User user){
        userMapper.updateById(user);
        return LayuiResult.updateSuccess();
    }

    @RequestMapping("/get")
    public Object get(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<User> users = userMapper.selectList(queryWrapper);
        Integer count = userMapper.selectCount(queryWrapper);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        pageInfo.getList().forEach(user -> {
            Integer roleId = user.getRoleId();
            user.setRoleName(roleMapper.selectById(roleId).getRoleName());
            user.setRoleDesc(roleMapper.selectById(roleId).getRoleDesc());
        });
        return LayuiResult.selectSuccess(pageInfo.getList(),count);
    }


    @RequestMapping("/getAll")
    public Object getAll(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<User> users = userMapper.selectList(queryWrapper);
        return LayuiResult.selectSuccess(users);
    }

    @RequestMapping("/login")
    public Object login(@RequestBody User user, HttpServletRequest request){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).eq("user_name",user.getUserName()).eq("user_password",user.getUserPassword());
        List<User> users = userMapper.selectList(queryWrapper);
        if(users.size() > 0){
            request.getSession().setAttribute("user",users.get(0));
            return LayuiResult.LayuiData(0,"登陆信息验证成功","/");
        }
        return LayuiResult.LayuiData(301,"登陆信息验证失败","/front/login");
    }


}
